package com.maserb.monitor.service.jwEducation.impl;

import com.maserb.monitor.entity.JwOrgEntity;
import com.maserb.monitor.entity.UserEntity;
import com.maserb.monitor.entity.schoolOrg.SchoolUserOrgEntity;
import com.maserb.monitor.repository.JwOrgRepository;
import com.maserb.monitor.repository.schoolOrg.SchoolUserOrgRepository;
import com.maserb.monitor.service.base.impl.JpaSpecificationServiceImpl;
import com.maserb.monitor.service.jwEducation.JwOrgService;
import com.maserb.monitor.service.remote.EcCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class JwOrgServiceImpl extends JpaSpecificationServiceImpl<JwOrgRepository,
        JwOrgRepository, JwOrgEntity, String> implements JwOrgService {

    @Autowired
    private SchoolUserOrgRepository userOrgRepository;

    @Autowired
    private EcCardService cardService;

    @Override
    public Page<JwOrgEntity> findAll(Pageable pageable, String name) {
        return this.findAll(new Specification<JwOrgEntity>() {
            @Override
            public Predicate toPredicate(Root<JwOrgEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<String> pathName = root.get("name");

                ArrayList<Predicate> list = new ArrayList<Predicate>();
                if (!StringUtils.isEmptyOrWhitespace(name)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.like(pathName, "%" + name + "%")));
                }
                return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
            }
        }, pageable);
    }



    @Override
    public List<JwOrgEntity> findByName(String name) {
        return repository.findByName(name);
    }

    @Override
    public JwOrgEntity findByDid(Long did) {
        return repository.findFirstByDid(did);
    }

    @Override
    public List<JwOrgEntity> findByType(String type) {
        return this.repository.findByTypeOrderByTreeLevel(type);
    }

    @Override
    public List<JwOrgEntity> findByTreeLevel(int treeLevel) {
        return repository.findByTreeLevel(treeLevel);
    }

    @Override
    public List<JwOrgEntity> findByUser(UserEntity user) {
        List<SchoolUserOrgEntity>userOrgList = userOrgRepository.findByUser(user);
        List<JwOrgEntity> clazzList = new ArrayList<JwOrgEntity>();
        if(null!=userOrgList && userOrgList.size()>0){
            Iterator<SchoolUserOrgEntity> iterator = userOrgList.iterator();
            while(iterator.hasNext()){
                SchoolUserOrgEntity userOrgEntity = iterator.next();
                clazzList.add(userOrgEntity.getOrg());
            }
        }
        return clazzList;
    }


    @Override
    public void delete(String var1) {
        JwOrgEntity org = this.findOne(var1);
        cardService.doDeleteOrg(org);
        org.setIsDel("1");
        this.save(org);
    }
}
